﻿using System;
using System.Collections.Generic;
using System.Text;
using ChromiumUpdater.Data;
using System.Configuration;

namespace ChromiumUpdater
{
    public static class ConfigManager
    {
        private static System.Configuration.Configuration appSettings = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        
        /// <summary>
        /// Loads settings from config file to be used in main application
        /// </summary>
        /// <returns>ConfigSettings if all settings loaded correctly, NULL if an error occurs</returns>
        public static ConfigSettings LoadConfig()
        {
            ConfigSettings conf = new ConfigSettings();

            try
            {
                conf.autoUpdateHours = Convert.ToInt32(appSettings.AppSettings.Settings["AutoUpdateHours"].Value);
                conf.autoUpdateMode = Convert.ToInt32(appSettings.AppSettings.Settings["AutoUpdateMode"].Value);
                conf.chromePath = appSettings.AppSettings.Settings["ChromePath"].Value.ToString();
                conf.currentVersion = appSettings.AppSettings.Settings["CurrentVersion"].Value.ToString();
                conf.proxyEnabled = Convert.ToBoolean(appSettings.AppSettings.Settings["ProxyEnabled"].Value);
                conf.proxyPort = appSettings.AppSettings.Settings["ProxyPort"].Value.ToString();
                conf.proxyURL = appSettings.AppSettings.Settings["ProxyUrl"].Value.ToString();
            }
            catch
            {
                return null;
            }

            return conf;        
        }

        /// <summary>
        /// Saves all settings from application to config file
        /// </summary>
        /// <param name="conf">ConfigSettings from main app</param>
        /// <returns>True if succesful, false if failed</returns>
        public static bool SaveConfig(ConfigSettings conf)
        {
            try
            {
                appSettings.AppSettings.Settings.Remove("CurrentVersion");
                appSettings.AppSettings.Settings.Add("CurrentVersion", conf.currentVersion.ToString());

                appSettings.AppSettings.Settings.Remove("ChromePath");
                appSettings.AppSettings.Settings.Add("ChromePath", conf.chromePath);

                appSettings.AppSettings.Settings.Remove("ProxyEnabled");
                appSettings.AppSettings.Settings.Add("ProxyEnabled", conf.proxyEnabled.ToString());

                appSettings.AppSettings.Settings.Remove("ProxyUrl");
                appSettings.AppSettings.Settings.Add("ProxyUrl", conf.proxyURL);

                appSettings.AppSettings.Settings.Remove("ProxyPort");
                appSettings.AppSettings.Settings.Add("ProxyPort", conf.proxyPort);

                appSettings.AppSettings.Settings.Remove("AutoUpdateMode");
                appSettings.AppSettings.Settings.Add("AutoUpdateMode", conf.autoUpdateMode.ToString());

                appSettings.AppSettings.Settings.Remove("AutoUpdateHours");
                appSettings.AppSettings.Settings.Add("AutoUpdateHours", conf.autoUpdateHours.ToString());

                appSettings.Save(ConfigurationSaveMode.Modified, true);
                ConfigurationManager.RefreshSection("configuration");
                
                return true;
            }

            catch
            {
                return false;
            }
        }
    }
}
